package com.ruoyi.grade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.grade.domain.entity.StudentPhysicalGrade;
import com.ruoyi.grade.mapper.StudentPhysicalGradeMapper;
import com.ruoyi.grade.service.IStudentPhysicalGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 体测成绩查询服务实现
 */
@Service
public class StudentPhysicalGradeServiceImpl
        extends ServiceImpl<StudentPhysicalGradeMapper, StudentPhysicalGrade>
        implements IStudentPhysicalGradeService {

    @Autowired
    private StudentPhysicalGradeMapper studentPhysicalGradeMapper;

    /**
     * 根据学号和学年查询体测成绩
     * @param studentId 学生ID
     * @return 体测成绩实体
     */
    public List<StudentPhysicalGrade> getPhysicalGrade(String studentId) {
        return lambdaQuery()
                .eq(StudentPhysicalGrade::getStudentId, studentId)
                .list();
    }

    @Override
    public String importStudentPhysicalGrade(List<StudentPhysicalGrade> studentPhysicalGradeList, Boolean updateSupport) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (StudentPhysicalGrade studentPhysicalGrade : studentPhysicalGradeList) {
            if(!checkStudentPhysicalGradeExist(studentPhysicalGrade.getStudentId(), studentPhysicalGrade.getAcademicYear())){
                //体测成绩不存在
                //调用保存业务
                studentPhysicalGradeMapper.insert(studentPhysicalGrade);

                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + studentPhysicalGrade.getStudentId() + " 导入成功");
            }   else if (updateSupport){
                //体测成绩存在
                //调用修改业务业务
                LambdaQueryWrapper<StudentPhysicalGrade> lqw = new LambdaQueryWrapper<>();
                lqw.eq(StudentPhysicalGrade::getStudentId, studentPhysicalGrade.getStudentId());
                lqw.eq(StudentPhysicalGrade::getAcademicYear, studentPhysicalGrade.getAcademicYear());
                studentPhysicalGradeMapper.update(studentPhysicalGrade, lqw);

                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " +  studentPhysicalGrade.getStudentId() + " 导入成功");
            } else{
                //账号存在且不支持更新
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、账号 " + studentPhysicalGrade.getStudentId() + " 已存在");
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return failureNum == 0? failureMsg.toString(): successMsg.toString();
    }

    @Override
    public Boolean checkStudentPhysicalGradeExist(String studentId, String academicYear) {
        //设置查询条件
        LambdaQueryWrapper<StudentPhysicalGrade> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StudentPhysicalGrade::getStudentId, studentId);
        lqw.eq(StudentPhysicalGrade::getAcademicYear, academicYear);
        //调用查询业务
        return studentPhysicalGradeMapper.selectOne(lqw) != null;
    }

}